﻿//Encryption methods courtesy of Josh Harrison:
//hxxp://www.joshrharrison.com/archive/2009/01/28/c-encryption.aspx

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace melodyMe
{
    public class Encryption
    {
        SymmetricAlgorithm symAlgorithm = DES.Create();

        /* DEVELOPER NOTE: Why Encrypt The Playlists?
         * I have encrypted the playlist data with a Rijndael ciper to prevent data corruption
         * as well as a prevention of adverse operations from within the program due to loss of integrity.
         * 
         * This should be seen in NO WAY as a method of proprietation. */

        public string Encrypt(string ClearText)
        {

            byte[] clearTextBytes = Encoding.UTF8.GetBytes(ClearText);

            SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();

            MemoryStream ms = new MemoryStream();
            byte[] rgbIV = Encoding.ASCII.GetBytes("65482339415e5561");
            byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo");
            CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);

            cs.Write(clearTextBytes, 0, clearTextBytes.Length);
            cs.Close();

            return Convert.ToBase64String(ms.ToArray());
        }

        public string Decrypt(string EncryptedText)
        {
            byte[] encryptedTextBytes = Convert.FromBase64String(EncryptedText);

            MemoryStream ms = new MemoryStream();

            System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();


            byte[] rgbIV = Encoding.ASCII.GetBytes("65482339415e5561");
            byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo");

            CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV),
            CryptoStreamMode.Write);

            cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);

            cs.Close();

            return Encoding.UTF8.GetString(ms.ToArray());

        }
    }
}